<template>
    <view class="content"></view>
</template>
 
<script>
// #ifdef APP-PLUS
var main, receiver, filter;
var _codeQueryTag = false;
export default {
    data() {
        return {
            scanCode: ''
        };
    },
    created: function(option) {
    	let that = this;
        that.bindUserCode();
		// uni.setStorageSync("scancodedate","");
    },
    onHide: function() {
        this.stopScan();
    },
    destroyed: function() {
        /*页面退出时一定要卸载监听,否则下次进来时会重复，造成扫一次出2个以上的结果*/
        this.stopScan();
    },
    methods: {
		//打开扫描头的方法
		bindUserCode() {
			let that = this;
			that.initScan();
			that.startScan();
			uni.$emit('scancodedate', "scan-tool加载好了!");
		},
		initScan() {
			 // #ifdef APP-PLUS
		
			let that = this;
			
				//获取Android主Activity
				that.main = plus.android.runtimeMainActivity();
				//获取Android意图类
				let Intent = plus.android.importClass('android.content.Intent');
				//实例化意图
				let intent = new Intent();
				//定义意图，模拟按下L键，L键实际上是打开激光的物理键映射，由厂商提供
				intent.setAction('com.android.action.keyevent.KEYCODE_KEYCODE_SCAN_L_DOWN');
				//广播这个意图
				that.main.sendBroadcast(intent);
				//获取Android意图过滤类
				let IntentFilter = plus.android.importClass('android.content.IntentFilter');
				//实例化意图过滤
				that.filter = new IntentFilter();
				//获取扫码成功的意图广播
				that.filter.addAction('com.android.server.scannerservice.broadcast');
				that.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getStringExtra('scannerdata');
						//成功输出扫码内容
						console.log("scan-tool输出扫码内容=>"+code);
						that.userCode = code
						// _this.queryCode(code);
						// that.resultCode(code)
						// uni.$emit('scancodedate',code)
						 uni.$emit("scancodedate",code)
						// uni.setStorageSync("scancodedate",code);
						// console.log("缓存同步:"+uni.getStorageSync("scancodedate"))
					}
				});
			
			// #endif
		
		},
		startScan() {
			 // #ifdef APP-PLUS
			this.main.registerReceiver(this.receiver, this.filter);
			// #endif
		},
		
		
        // initScan() {
        //     let _this = this;
        //     main = plus.android.runtimeMainActivity(); //获取activity
        //     var IntentFilter = plus.android.importClass('android.content.IntentFilter');
        //     filter = new IntentFilter();
        //     filter.addAction('android.intent.ACTION_DECODE_DATA'); // 换你的广播动作
        //     receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
        //         onReceive: function(context, intent) {
        //             plus.android.importClass(intent);
        //             let code = intent.getStringExtra('barcode_string'); // 换你的广播标签
        //             _this.queryCode(code);
        //         }
        //     });
        // },
        // startScan() {
        //     main.registerReceiver(receiver, filter);
        // },
        stopScan() {
			// #ifdef APP-PLUS
			 main.unregisterReceiver(receiver);
			// #endif
           
        },
        queryCode: function(code) {
			// #ifdef APP-PLUS
			
            //防重复
            if (_codeQueryTag) return false;
            _codeQueryTag = true;
            setTimeout(function() {
                _codeQueryTag = false;
            }, 50);
            var id = code;
            uni.$emit('scancodedate', { code: id });
			// #endif
        },
    },
};
// #endif
</script>